from django.shortcuts import render, HttpResponse
from rest_framework.decorators import action
from rest_framework.viewsets import GenericViewSet
from apps.backend.serializer import UserSerializer
from apps.backend.models import User
from common import CommonResponse, UserErrorCode
from rest_framework import status
from toolkit import DesensitizeData

from celery_task.user_register_task import user_register


# Create your views here.
class CommonViewSet(GenericViewSet):
    def check_user(self, request, username, *args, **kwargs):
        if username not in request.bloom:
            return False
        else:
            return True


class GetUsername(CommonViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer

    @action(methods=['get'], detail=False)
    def check_user_exist(self, request, username, *args, **kwargs):
        user_exist = self.check_user(request, username)
        if not user_exist:
            return CommonResponse().success(code='1',
                                            message=f"当前用户名 {username} 不存在!",
                                            )
        else:
            return CommonResponse().failure(
                code=UserErrorCode.USER_USERNAME_EXIST_ERROR.code(),
                message=UserErrorCode.USER_USERNAME_EXIST_ERROR.message(),

            )

    def __get_user_by_username(self, username, actual=False):
        queryset = User.objects.filter(username=username)
        if queryset:
            queryset = queryset.first()
            serializer = self.get_serializer(queryset, many=False)
            clean_data = serializer.data
            if not actual:
                clean_data["phone"] = DesensitizeData(original_data=clean_data["phone"]).desensitize
            return CommonResponse().success(code='1',
                                            message=f"查询当前用户 {username} 信息成功!",
                                            data=clean_data
                                            )
        else:
            return CommonResponse().failure(code=UserErrorCode.USER_USERNAME_NULL.code(),
                                            message=UserErrorCode.USER_USERNAME_NULL.message(),
                                            back_status=status.HTTP_404_NOT_FOUND)

    @action(methods=['get'], detail=False)
    def search_user(self, request, username, *args, **kwargs):
        return self.__get_user_by_username(username=username, actual=False)

    @action(methods=['get'], detail=False)
    def search_actual_user(self, request, username, *args, **kwargs):
        return self.__get_user_by_username(username=username, actual=True)


class RegisterView(CommonViewSet):
    @action(methods=['post'], detail=False)
    def create_user(self, request, *args, **kwargs):
        # 异步使用
        res = user_register.delay()

        print(res)
        if request.method == 'POST':
            username = request.data.get('username')
            user_exist = self.check_user(request=request, username=username)
            # 判断当前用户名是否存在
            if user_exist:
                return CommonResponse().failure(
                    code=UserErrorCode.USER_USERNAME_EXIST_ERROR.code(),
                    message=UserErrorCode.USER_USERNAME_EXIST_ERROR.message()
                )
            # 用户名不存在的情况下注册用户信息
            serializer = UserSerializer(data=request.data)
            # 校验前端传入的个人数据
            if not serializer.is_valid():
                return CommonResponse().failure(
                    code=UserErrorCode.USER_PARAM_ERROR.code(),
                    message=UserErrorCode.USER_PARAM_ERROR.message()
                )
            # serializer.save()
            return CommonResponse().success(
                code="1",
                message=f"用户 {username} 注册成功"
            )
